﻿using FriendStar.Code;
using FriendStar.Domain.Entity.VBaseData;
using FriendStar.Domain.IRepository.VBaseData;
using FriendStar.Repository.VBaseData;
using System.Collections.Generic;
using System.Linq;
using SqlSugarRepository;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace FriendStar.Application.VBaseData
{
    public class V_CPK_A2_BB_StoProStockCata_LJCRApp
    {
        private IV_CPK_A2_BB_StoProStockCata_LJCRRepository service = new V_CPK_A2_BB_StoProStockCata_LJCRRepository();
        //生产单位fhappenComNO 零件编号fproductionNo 	供货单位fproCompanyName	 零件分类fproductionTypeName 台账日期faccountDate 
        public List<V_CPK_A2_BB_StoProStockCata_LJCREntity> GetList(Pagination pagination, string fhappenComNO, string fproductionNo, string FProCompanyNO, string fproductionTypeName, string faccountDate, string ftype)
        {
            var expression = ExtLinq.True<V_CPK_A2_BB_StoProStockCata_LJCREntity>();
            if (!string.IsNullOrEmpty(fhappenComNO))
            {
                expression = expression.And(t => t.FHappenComNO.Contains(fhappenComNO));
            }
            if (!string.IsNullOrEmpty(fproductionNo))
            {
                expression = expression.And(t => t.FProductionNO.Contains(fproductionNo));
            }
            if (!string.IsNullOrEmpty(FProCompanyNO))
            {
                expression = expression.And(t => t.FProCompanyNO.Contains(FProCompanyNO));
            }
            if (!string.IsNullOrEmpty(fproductionTypeName))
            {
                expression = expression.And(t => t.FProductionTypeName.Contains(fproductionTypeName));
            }
            if (!string.IsNullOrEmpty(faccountDate))
            {
                expression = expression.And(t => t.FAccountDate.Contains(faccountDate));
            }
            if (!string.IsNullOrEmpty(ftype))
            {
                expression = expression.And(t => t.FType.Contains(ftype));
            }
            return service.FindList(expression, pagination).OrderBy(t => t.UseUnit).ToList();
        }

        public List<Dictionary<string, object>> GetListH(Pagination pagination, string fhappenComNO, string fproductionNo, string FProCompanyNO, string fproductionTypeName, string faccountDate, string ftype,string unitcode)
        {
            List<Dictionary<string, object>> result = null;

            int start = 0, end = 0;

            string sql = @"select * from(SELECT row_number () over (order by FDocuListID) num,* from {3}) a
            where  num > {0} and num <= {1}  and useUnit = @useUnit {2}", tableName = "V_CPK_A2_BB_StoProStockCata_LJCR";


            StringBuilder conn = null;

            Dictionary<string, object> item = null;

            result = new List<Dictionary<string, object>>();

            conn = new StringBuilder();


            if (!string.IsNullOrEmpty(faccountDate))
            {
                conn.Append(string.Format(" and FAccountDate = '{0}'", faccountDate));
            }

            if (!string.IsNullOrEmpty(ftype))
            {
                conn.Append(string.Format(" and ftype = '{0}'", ftype));
            }

            if (!string.IsNullOrEmpty(fhappenComNO))
            {
                conn.Append(string.Format(" and fhappenComNO = '{0}'", fhappenComNO));
            }

            if (!string.IsNullOrEmpty(fproductionNo))
            {
                conn.Append(string.Format(" and fproductionNo = '{0}'", fproductionNo));
            }

            if (!string.IsNullOrEmpty(FProCompanyNO))
            {
                conn.Append(string.Format(" and FProCompanyNO = '{0}'", FProCompanyNO));
            }

            start = (pagination.page - 1) * pagination.rows;

            end = pagination.page * pagination.rows;

            sql = string.Format(sql, start, end, conn, tableName);

            Common.GetRecords(pagination, tableName, conn);

            SqlParameter[] pars = new SqlParameter[] {
                new SqlParameter("@useUnit", unitcode),
                new SqlParameter("@FAccountDate", faccountDate)
            };
            using (SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.GetConnection(), CommandType.Text, sql, pars))
            {
                while (reader.Read())
                {
                    item = new Dictionary<string, object>();

                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        item.Add(reader.GetName(i), reader[i]);
                    }

                    result.Add(item);
                }
            }

            return result;
        }

    }
}
